<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const username = ref('');
const password = ref('');
const captchaInput = ref('');
const result = ref('');
const usernameerror = ref('');
const currentCode = ref('');

// 登录功能
const login = () => {
  if (captchaInput.value.toLowerCase() === currentCode.value.toLowerCase()) {
    result.value = "✅ 验证成功";
    
    if (username.value === "test" && password.value === "123456") {
      // 验证用户名和密码
      localStorage.setItem('isLoggedIn', 'true');
      router.push('/');
    } else {
      usernameerror.value = "❌ 用户名或者密码错误";
    }
  } else {
    result.value = "❌ 验证失败，请重试";
  }
};

// 随机数生成函数
const rand = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

// 生成验证码
const drawCaptcha = () => {
  const canvas = document.getElementById("captchaCanvas");
  if (!canvas) return;
  
  const ctx = canvas.getContext("2d");
  const chars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
  
  ctx.fillStyle = "#fff";
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  currentCode.value = "";

  // 绘制随机字符
  for (let i = 0; i < 5; i++) {
    const ch = chars[rand(0, chars.length - 1)];
    currentCode.value += ch;
    ctx.save();
    ctx.font = "bold 28px sans-serif";
    ctx.fillStyle = `rgb(${rand(0,150)},${rand(0,150)},${rand(0,150)})`;
    ctx.translate(20 + i * 25, 30);
    ctx.rotate(rand(-20, 20) * Math.PI / 180);
    ctx.fillText(ch, 0, 0);
    ctx.restore();
  }

  // 干扰线
  for (let i = 0; i < 5; i++) {
    ctx.strokeStyle = `rgb(${rand(100,200)},${rand(100,200)},${rand(100,200)})`;
    ctx.beginPath();
    ctx.moveTo(rand(0, canvas.width), rand(0, canvas.height));
    ctx.lineTo(rand(0, canvas.width), rand(0, canvas.height));
    ctx.stroke();
  }

  // 干扰点
  for (let i = 0; i < 40; i++) {
    ctx.fillStyle = `rgb(${rand(0,255)},${rand(0,255)},${rand(0,255)})`;
    ctx.fillRect(rand(0, canvas.width), rand(0, canvas.height), 1, 1);
  }
};

// 初始化验证码和添加事件监听
onMounted(() => {
  const canvas = document.getElementById("captchaCanvas");
  if (canvas) {
    canvas.addEventListener("click", drawCaptcha);
    drawCaptcha();
  }
});
</script>

<template>
  <div class="login-container">
    <div class="login-form">
      <h1>荣科智能生产系统</h1>
      <div class="form-group">
        <label>用户名</label>
        <input type="text" class="form-control" placeholder="请输入用户名" v-model="username">
        <div class="error-message" v-if="usernameerror">{{ usernameerror }}</div>
      </div>
      <div class="form-group">
        <label>密码</label>
        <input type="password" class="form-control" placeholder="请输入密码" v-model="password">
      </div>
      <div class="form-group">
        <label>验证码</label>
        <div class="captcha-container">
          <input type="text" class="form-control" placeholder="请输入验证码" v-model="captchaInput">
          <canvas id="captchaCanvas" width="140" height="48" title="点击刷新"></canvas>
        </div>
        <div class="result-message" :class="{ 'success': result.includes('✅'), 'error': result.includes('❌') }">{{ result }}</div>
      </div>
      <button class="btn" @click="login">登录</button>
    </div>
  </div>
</template>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

/* 登录页面样式 */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-form {
  background: white;
  padding: 2.5rem;
  border-radius: 10px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h1 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #2c80ff;
  font-size: 1.8rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.form-control {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #dee2e6;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-control:focus {
  border-color: #2c80ff;
  outline: none;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-container input {
  flex: 1;
}

.btn {
  display: inline-block;
  padding: 0.75rem 1.5rem;
  background-color: #2c80ff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: background-color 0.3s;
  text-align: center;
  width: 100%;
}

.btn:hover {
  background-color: #1a6de0;
}

.error-message {
  color: red;
  margin-top: 5px;
  font-size: 14px;
}

.result-message {
  margin-top: 5px;
  font-size: 14px;
}

.success {
  color: green;
}

.error {
  color: red;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-form {
    padding: 1.5rem;
  }
}
</style>